package com.mojie.overscroll.demo.recyclerview

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.mojie.overscroll.demo.ItemView
import com.mojie.overscroll.demo.databinding.ItemViewBinding

/**
 *
 * @Author:         lxp
 * @CreateDate:     2024/8/8
 */
class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val data = mutableListOf<Int>()

    @SuppressLint("NotifyDataSetChanged")
    fun freshData(size: Int) {
        data.clear()
        repeat(size) {
            data.add(it + 1)
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val binding = ItemViewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder !is ViewHolder){
            return
        }
        holder.bindData(data[position])
    }

    private class ViewHolder(private val binding: ItemViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(data:Int){
            binding.tv.text = data.toString()
        }
    }
}